<template>
  <!-- 路由传值
         传：this.$router.push('/xxx?参数名=值')
         收: this.$route.query.参数名  -->

  <!--  this.$router.back()
      this.$router.back(1)  前进一页
      this.$router.back(-1) 后退一页
      this.$router.back(0)  刷新当前页面
 -->
  <!-- 渲染详情数据
      1：定义接口api
      2:导入api
      3:进入页面调用api
      4:调用完成存储数据并渲染  -->

  <!--   点赞与取消点赞接口调用
         1：定义接口api
         2:导入api
         3:点击后调用，
         4：调用完成修改状态 -->

  <div class="detail-page">
    <van-nav-bar
      left-text="返回"
      @click-left="$router.push(back)"
      fixed
      title="文章详情"
    />
    <header class="header">
      <h1>{{ info.stem }}</h1>
      <p>
        {{ info.creator }} | {{ info.views }} 浏览量 |
        {{ info.likeCount }} 点赞数
      </p>
      <p>
        <img :src="info.avatar" alt="" />
        <span>{{ info.creator }}</span>
      </p>
    </header>
    <main class="body" v-html="info.content"></main>
    <div class="opt">
      <van-icon
        name="like-o"
        @click="likeClick"
        :class="{ active: info.likeFlag === 1 }"
      />
      <van-icon
        @click="collectClick"
        name="star-o"
        :class="{ active: info.collectFlag === 1 }"
      />
    </div>
  </div>
</template>

<script>
import { interviewShow, interviewOpt } from '@/api/detail'
export default {
  name: 'detail-page',
  data () {
    return {
      id: this.$route.query.id,
      back: this.$route.query.back,
      info: ''
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await interviewShow(this.id)
      this.info = res.data
      console.log(res)
    },
    // 点赞点击事件
    async likeClick () {
      await interviewOpt({
        id: this.id,
        optType: 1
      })
      // 改变状态
      this.info.likeFlag = 1 - this.info.likeFlag
      // 提示一下
      this.$toast.success(this.info.likeFlag ? '点赞成功' : '取消点赞成功')
      if (this.info.likeFlag) {
        this.info.likeCount++
      } else {
        this.info.likeCount--
      }
    },
    async collectClick () {
      await interviewOpt({
        id: this.id,
        optType: 2
      })
      // 改变状态
      this.info.collectFlag = 1 - this.info.collectFlag
      // 提示一下
      this.$toast.success(this.info.collectFlag ? '收藏成功' : '取消收藏成功')
    }
  }
}
</script>

<style lang="less" scoped>
.detail-page {
  margin-top: 44px;
  overflow: hidden;
  padding: 0 15px;
  .header {
    h1 {
      font-size: 24px;
    }
    p {
      color: #999;
      font-size: 12px;
      display: flex;
      align-items: center;
    }
    img {
      width: 40px;
      height: 40px;
      border-radius: 50%;
      overflow: hidden;
    }
  }
  .opt {
    position: fixed;
    bottom: 100px;
    right: 0;
    > .van-icon {
      margin-right: 20px;
      background: #fff;
      width: 40px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 50%;
      box-shadow: 2px 2px 10px #ccc;
      font-size: 18px;
      &.active {
        background: #fec635;
        color: #fff;
      }
    }
  }
}
</style>
